﻿package com.sos.ui
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	public class EffectIcon extends MovieClip
	{
		public var effect:Effect;
		public var box:EffectBox;
		
		public var targetMode:Boolean;
		public var isValidTarget:Boolean;
		
		public function EffectIcon(effect:Effect, box:EffectBox, iconSize:String) {
			
			this.effect = effect;
			this.box = box;
			this.buffIcon.visible = false;
			this.debuffIcon.visible = false;
			this.gearIcon.visible = false;
			this.targeted.visible = false;
			this.genericIcon.visible = false;
			showIcon();
			
			if (iconSize == 'large') {
				this.addEventListener(MouseEvent.ROLL_OVER,handleMouseListeners,false,0,true);
				this.addEventListener(MouseEvent.ROLL_OUT,handleMouseListeners,false,0,true);
				this.addEventListener(MouseEvent.CLICK, handleMouseListeners, false, 0, true);
			}
		}
		
		public function showIcon():void {
			switch (effect.type) {
				case 'Gear':
					this.gearIcon.visible = true;
					break;
				case 'Buff':
					this.buffIcon.visible = true;
					break;
				case 'Debuff':
					this.debuffIcon.visible = true;
					break;
				default:
					this.genericIcon.visible = true;
					break;
			}
			
		}
		
		function enterTargetMode(isValidTarget:Boolean) {
			//this.box.basePlayer.ignoreLeftClick = true;
			targetMode = true;
			
			if (isValidTarget == true) {
				this.buttonMode = true;
				this.useHandCursor = true;
				this.isValidTarget = true;
			}
			else {
				//filters = [this.greyOutFilter];
			}
			
		}
		
		function exitTargetMode() {
			//trace("ICON EXITING TARGET MODE: ", effect.effectName);
			//this.box.basePlayer.ignoreLeftClick = false;
			if (isValidTarget == true) {
				this.buttonMode = false;
				this.useHandCursor = false;
			}
			targetMode = false;
			isValidTarget = false;
			
			//this.box.basePlayer.ignoreClick = false;
			filters = [];
		}
		
		public function handleMouseListeners(e:MouseEvent) {
			
			switch (e.type) {
				case "rollOver":
					box.basePlayer.rolloverEnabled = false;
					box.effectDetailBox.show(effect);
					/*
					box.effectDetailBox.effectName.text = effect.effectName;
					box.effectDetailBox.effectText.text = effect.effectText + " (Id: " + effect.effectId + ")";
					box.effectDetailBox.visible = true;
					*/
					//trace("ROLLOVER EFFECTBOX");
					break;
				case "rollOut":
					box.basePlayer.rolloverEnabled = true;
					box.effectDetailBox.hide();
					//box.effectDetailBox.visible = false;
					break;	
				case "click":
					trace("CLICK EFFECTICON");
					
					if (isValidTarget == true && targetMode == true)
					{
						if (box.basePlayer.playArea.targetingCardId != '') {
							trace(effect.effectName + " TARGETED");
							box.basePlayer.playArea.handCardList[box.basePlayer.playArea.targetingCardId].setTarget(effect.effectId);
							//this.targeted.visible = true;
							//box.smallIconList[effect.effectId].targeted.visible = true;
							e.stopImmediatePropagation();
						}
						else {
							
						}
					}
					break;	
			}
			
		}
	}
}